package org.advanced.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @author John.zhang
 * @version Id: MyProducer, v 1.0 2020/11/26 17:32 ZhiYong Exp $
 */
public class MyProducer {
    private final static String EXCHANGE_NAME = "SIMPLE_EXCHANGE";
    private final static String QUEUE_NAME = "SIMPLE_QUEUE";

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        // 连接IP
        factory.setHost("10.181.144.254");
        // 虚拟机
        factory.setVirtualHost("/");
        // 连接端口
        factory.setPort(5672);

        // 用户
        factory.setUsername("admin");
        factory.setPassword("admin");

        // 建立连接
        Connection conn = factory.newConnection();
        // 创建消息通道
        Channel channel = conn.createChannel();

        // 发送消息
        String msg = "Hello world, Rabbit MQ";

        // String exchange, String routingKey, BasicProperties props, byte[] body
        channel.basicPublish(EXCHANGE_NAME, "gm.best", null, msg.getBytes());
        //将消息发给默认的交换机，并且把消息发给指定队列
        //channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());

        channel.close();
        conn.close();
    }

}
